package ilincar.systemcore.model;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import ilincar.systemcore.R;
import ilincar.systemcore.tool.ILog;

/**
 * Created by ilincar on 2016/12/16.
 * 微信接人，根据DDPUSH发过来的数据，显示对话框，打开高德
 */

public class WxMeetPersion {

    private final String TAG = "WxMeetPersion";

    private Context mContext = null;

    private Dialog mDialog = null;

    public WxMeetPersion(@NonNull Context context) {
        this.mContext = context;
        initialize();
    }

    private Button mBtnSure;
    private Button mBtnCancel;
    private TextView mAccessText; //接人信息

    private JSONObject mCurJsonObject = null;

    //private AccessCallback mAccessCallback = null;

    /**
     * 接人信息回调接口
     */
//    public interface AccessCallback{
//        void onAccessSure();
//    }
    private void initialize() {
        mDialog = new Dialog(mContext, R.style.wxdialog);
        mDialog.setContentView(R.layout.wx_dialog);
        mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        mBtnSure = (Button) mDialog.findViewById(R.id.btn_sure);
        mBtnSure.setOnClickListener(onClickListener);
        mBtnCancel = (Button) mDialog.findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(onClickListener);
        mAccessText = (TextView) mDialog.findViewById(R.id.access_text);
    }

    /**
     * 显示微信接人对话框，解析JsonObject 数据
     *
     * @param jsonObject
     */
    public void show(JSONObject jsonObject) {
        if (jsonObject == null) {
            return;
        }
        try {
            mCurJsonObject = jsonObject;
            if (mCurJsonObject.has("endPointName")) {
                mAccessText.setText(mCurJsonObject.getString("endPointName"));
                if (!mDialog.isShowing()) {
                    mDialog.show();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void requestPrenavigation(JSONObject jsonObject) {
        if (jsonObject != null) {
            ILog.d(TAG, "请求预导航 : " + jsonObject.toString());
            String endPointName = "";
            String endPointLatitude = "";
            String endPointLongitude = "";
            String preferenceType = "";
            try {
                if (jsonObject.has("endPointName")) {
                    endPointName = jsonObject.getString("endPointName");
                }
                if (jsonObject.has("endPointLatitude")) {
                    endPointLatitude = jsonObject.getString("endPointLatitude");
                }
                if (jsonObject.has("endPointLongitude")) {
                    endPointLongitude = jsonObject.getString("endPointLongitude");
                }
                if (jsonObject.has("preferenceType")) {
                    preferenceType = jsonObject.getString("preferenceType");
                }
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("androidauto://navi?sourceApplication=txz&poiname=" + endPointName +
                        "&lat=" + endPointLatitude + "&lon=" + endPointLongitude + "&dev=0&style=" + preferenceType));
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setPackage("com.autonavi.amapautolite");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_sure:
                    mDialog.dismiss();
                    if(mCurJsonObject != null){
                        requestPrenavigation(mCurJsonObject);
                        mCurJsonObject = null;
                    }
                    break;
                case R.id.btn_cancel:
                    mDialog.dismiss();
                    break;
            }
        }
    };
}
